package com.wgy.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgy.blog.common.Result;
import com.wgy.blog.mapper.SortNavMapper;
import com.wgy.blog.pojo.SortNav;
import com.wgy.blog.service.SortNavService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:Iny
 * @Data：2021/11/15 9:45
 */
@Service
public class SortNavServiceImpl implements SortNavService {
    @Resource
    private SortNavMapper sortNavMapper;

    @Override
    public Result<List<SortNav>> findAll(PageInfo pageInfo) {
        PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getPageSize());
        List<SortNav> list = sortNavMapper.findAll();
        PageInfo<SortNav> info = new PageInfo<>(list);

        return Result.success((int) info.getTotal(),info.getList());
    }

    @Override
    public Result update(SortNav sortNav) {
        int update = sortNavMapper.update(sortNav);
        if (update>0){
            return Result.success(update);
        }
        return Result.fail();
    }

    @Override
    public Result add(SortNav sortNav) {
        SortNav byName = sortNavMapper.findByName(sortNav.getSortName());
        if (byName!=null){
            return Result.addFail();
        }
        int add = sortNavMapper.add(sortNav);
        if (add>0){
            return Result.addSuccess();
        }
       return Result.fail();
    }

    @Override
    public Result deleteSortNav(int id) {
        int delete = sortNavMapper.deleteById(id);
        if (delete > 0){
           return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result updateIsShow(int sortId) {
        int updateIsShow = sortNavMapper.updateIsShow(sortId);
        if (updateIsShow > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result findByIsShow() {
        List<SortNav> sortNavList = sortNavMapper.findByIsShow();
        return Result.success(sortNavList);
    }
}
